package org.creavi.engine.hud;

import java.util.HashMap;

import org.creavi.engine.compiler.ResourceCompiler;

public class HudDefinitionFactory {
	private static HudDefinitionFactory hudFactory;
	
	public static HudDefinitionFactory getInstance() {
		if (hudFactory == null) {
			hudFactory = new HudDefinitionFactory();
		}
		
		return hudFactory;
	}
	
	private HashMap<String, HudDefinition> huds;
	private HudDefinitionLoader loader;
	
	private HudDefinitionFactory() {
		huds = new HashMap<String, HudDefinition>();
		loader = new HudDefinitionLoader();
	}
	
	public HudDefinition getHudDefinition(String name) {
		if (huds.containsKey(name)) {
			return huds.get(name);
		} else {
			synchronized (huds) {
				HudDefinition hud = buildHud(name);
				
				huds.put(name, hud);
				
				return hud;
			}
		}
	}

	private HudDefinition buildHud(String name) {
		return (HudDefinition)ResourceCompiler.getInstance().loadResource(name, loader);
	}
}
